package com.glitter.player.springbootmultipledatasource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/**
 * @program:
 * @author: Player
 * @create: 2019-08-15
 **/
@Component
@Slf4j
public class JDBCRunner implements CommandLineRunner {

    @Autowired
    private JdbcTemplate headJdbcTemplate;

    @Autowired
    private JdbcTemplate footJdbcTemplate;

    private void showHeadData(){
        headJdbcTemplate.queryForList("SELECT * FROM HEAD_GLITTER")
                .forEach(row->log.info("HeadData:{}",row.toString()));
    }


    private void showFootData(){
        footJdbcTemplate.queryForList("SELECT * FROM FOOT_GLITTER")
                .forEach(row->log.info("FootData:{}",row.toString()));
    }

    private void initSchema(){
        headJdbcTemplate.execute("CREATE TABLE HEAD_GLITTER(ID INT IDENTITY, NAME VARCHAR(64));");
        headJdbcTemplate.execute("INSERT INTO HEAD_GLITTER(ID,NAME) VALUES (1,'PLAYER');INSERT INTO HEAD_GLITTER(ID,NAME) VALUES (2,'FULINLIN')");
        footJdbcTemplate.execute("CREATE TABLE FOOT_GLITTER(ID INT IDENTITY, NAME VARCHAR(64));");
        footJdbcTemplate.execute("INSERT INTO FOOT_GLITTER(ID,NAME) VALUES (1,'PLAYER');INSERT INTO FOOT_GLITTER(ID,NAME) VALUES (2,'FULINLIN');");
    }


    @Override
    public void run(String... args) {
        initSchema();
        showHeadData();
        showFootData();
    }
}
